/**
 * Copyright (c) 2008, Andrew Carter All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. Neither the name of Andrew Carter nor the names of
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor.definitions;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.PropertySectionState;
import com.acarter.propertytable.PropertyTable;
import com.acarter.propertytable.propertyobject.EnumPropertyObject;
import com.acarter.propertytable.propertyobject.FloatPropertyObject;
import com.acarter.propertytable.propertyobject.EnumPropertyObject.I_EnumPropertyObjectListener;
import com.acarter.propertytable.propertyobject.FloatPropertyObject.I_FloatPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.A_PropertyPage;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject.I_QuaternionPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject.I_Vector3fPropertyObjectListener;
import com.jme.image.Texture;
import com.jme.image.Texture.ApplyMode;
import com.jme.image.Texture.CombinerFunctionAlpha;
import com.jme.image.Texture.CombinerFunctionRGB;
import com.jme.image.Texture.CombinerOperandAlpha;
import com.jme.image.Texture.CombinerOperandRGB;
import com.jme.image.Texture.CombinerScale;
import com.jme.image.Texture.CombinerSource;
import com.jme.image.Texture.EnvironmentalMapMode;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.image.Texture.RenderToTextureType;
import com.jme.image.Texture.WrapAxis;
import com.jme.image.Texture.WrapMode;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;

/**
 * @author Carter
 * 
 */
public class TexturePropertyPage extends A_PropertyPage {

	/**  */
	private Texture texture = null;

	/**
	 * 
	 */
	public TexturePropertyPage() {
		
		PropertySection section = new PropertySection("Texture");
		section.addProperty(new Property("Anisotropic Filter Percent", new FloatPropertyObject()));
		section.addProperty(new Property("Apply Mode", new EnumPropertyObject<ApplyMode>()));
		section.addProperty(new Property("Environmental Map Mode", new EnumPropertyObject<EnvironmentalMapMode>()));
		section.addProperty(new Property("Magnification Filter", new EnumPropertyObject<MagnificationFilter>()));
		section.addProperty(new Property("Minification Filter", new EnumPropertyObject<MinificationFilter>()));
		section.addProperty(new Property("Render To Texture Type", new EnumPropertyObject<RenderToTextureType>()));
		// TODO when supported   section.addProperty(new Property("Wrap Mode R", new EnumPropertyObject<WrapMode>()));
		section.addProperty(new Property("Wrap Mode S", new EnumPropertyObject<WrapMode>()));
		section.addProperty(new Property("Wrap Mode T", new EnumPropertyObject<WrapMode>()));
		section.addProperty(new Property("Translation", new Vector3fPropertyObject()));
		section.addProperty(new Property("Rotation", new QuaternionPropertyObject()));
		section.addProperty(new Property("Scale", new Vector3fPropertyObject()));
		
		section.setState(PropertySectionState.EXPANDED);
		model.addPropertySection(section);

		section = new PropertySection("Alpha Combine");
		section.addProperty(new Property("Combiner Funtion", new EnumPropertyObject<CombinerFunctionAlpha>()));
		section.addProperty(new Property("Combiner Source 0", new EnumPropertyObject<CombinerSource>()));
		section.addProperty(new Property("Combiner Operand 0", new EnumPropertyObject<CombinerOperandAlpha>()));
		section.addProperty(new Property("Combiner Source 1", new EnumPropertyObject<CombinerSource>()));
		section.addProperty(new Property("Combiner Operand 1", new EnumPropertyObject<CombinerOperandAlpha>()));
		section.addProperty(new Property("Combiner Source 2", new EnumPropertyObject<CombinerSource>()));
		section.addProperty(new Property("Combiner Operand 2", new EnumPropertyObject<CombinerOperandAlpha>()));
		section.addProperty(new Property("Combiner Scale", new EnumPropertyObject<CombinerScale>()));
		
		section.setState(PropertySectionState.EXPANDED);
		model.addPropertySection(section);

		section = new PropertySection("RGB Combine");
		section.addProperty(new Property("Combiner Funtion", new EnumPropertyObject<CombinerFunctionRGB>()));
		section.addProperty(new Property("Combiner Source 0", new EnumPropertyObject<CombinerSource>()));
		section.addProperty(new Property("Combiner Operand 0", new EnumPropertyObject<CombinerOperandRGB>()));
		section.addProperty(new Property("Combiner Source 1", new EnumPropertyObject<CombinerSource>()));
		section.addProperty(new Property("Combiner Operand 1", new EnumPropertyObject<CombinerOperandRGB>()));
		section.addProperty(new Property("Combiner Source 2", new EnumPropertyObject<CombinerSource>()));
		section.addProperty(new Property("Combiner Operand 2", new EnumPropertyObject<CombinerOperandRGB>()));
		section.addProperty(new Property("Combiner Scale", new EnumPropertyObject<CombinerScale>()));
		
		section.setState(PropertySectionState.EXPANDED);
		model.addPropertySection(section);
	}

	/**
	 * @see com.acarter.scenemonitor.propertydescriptor.A_PropertyPage#updateInfo(com.acarter.propertytable.PropertyTableModel,
	 *      java.lang.Object)
	 */
	@Override
	public void updateModel(PropertyTable table, Object object) {

		if(object instanceof Texture) {

			Texture texture = (Texture) object;

			if(this.texture == null || !this.texture.equals(texture)) {

				this.texture = texture;
				updateListeners(texture);
			}
		}
	}

	/**
	 * 
	 * @param table
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	protected void updateListeners(final Texture texture) {
		
		FloatPropertyObject aniso = (FloatPropertyObject)model.getPropertySection("Texture").getProperty("Anisotropic Filter Percent").getPropertyObject();
		
		aniso.SetListener(new I_FloatPropertyObjectListener() {

			public float readValue() {
				
				return texture.getAnisotropicFilterPercent();
			}

			public void saveValue(float value) {
				
				texture.setAnisotropicFilterPercent(value);
			}
		});
		
		EnumPropertyObject<ApplyMode> applyMode = (EnumPropertyObject<ApplyMode>)model.getPropertySection("Texture").getProperty("Apply Mode").getPropertyObject();
		
		applyMode.SetListener(new I_EnumPropertyObjectListener<ApplyMode>() {

			public ApplyMode readValue() {
				
				return texture.getApply();
			}

			public void saveValue(ApplyMode value) {
				
				texture.setApply(value);
			}
		});
		
		EnumPropertyObject<EnvironmentalMapMode> environmentalMap = (EnumPropertyObject<EnvironmentalMapMode>)model.getPropertySection("Texture").getProperty("Environmental Map Mode").getPropertyObject();
		
		environmentalMap.SetListener(new I_EnumPropertyObjectListener<EnvironmentalMapMode>() {

			public EnvironmentalMapMode readValue() {
				
				return texture.getEnvironmentalMapMode();
			}

			public void saveValue(EnvironmentalMapMode value) {
				
				texture.setEnvironmentalMapMode(value);
			}
		});
		
		EnumPropertyObject<MagnificationFilter> magnification = (EnumPropertyObject<MagnificationFilter>)model.getPropertySection("Texture").getProperty("Magnification Filter").getPropertyObject();
		
		magnification.SetListener(new I_EnumPropertyObjectListener<MagnificationFilter>() {

			public MagnificationFilter readValue() {
				
				return texture.getMagnificationFilter();
			}

			public void saveValue(MagnificationFilter value) {
				
				texture.setMagnificationFilter(value);
			}
		});
		
		EnumPropertyObject<MinificationFilter> minification = (EnumPropertyObject<MinificationFilter>)model.getPropertySection("Texture").getProperty("Minification Filter").getPropertyObject();
		
		minification.SetListener(new I_EnumPropertyObjectListener<MinificationFilter>() {

			public MinificationFilter readValue() {
				
				return texture.getMinificationFilter();
			}

			public void saveValue(MinificationFilter value) {
				
				texture.setMinificationFilter(value);
			}
		});
		
		EnumPropertyObject<RenderToTextureType> rtt = (EnumPropertyObject<RenderToTextureType>)model.getPropertySection("Texture").getProperty("Render To Texture Type").getPropertyObject();
		
		rtt.SetListener(new I_EnumPropertyObjectListener<RenderToTextureType>() {

			public RenderToTextureType readValue() {
				
				return texture.getRTTSource();
			}

			public void saveValue(RenderToTextureType value) {
				
				texture.setRenderToTextureType(value);
			}
		});
		/*// TODO enable this when supported
		EnumPropertyObject<WrapMode> wrapR = (EnumPropertyObject<WrapMode>)model.getPropertySection("Texture").getProperty("Wrap Mode R").getPropertyObject();
		
		wrapR.SetListener(new I_EnumPropertyObjectListener<WrapMode>() {

			public WrapMode readValue() {
				
				return texture.getWrap(WrapAxis.R);
			}

			public void saveValue(WrapMode value) {
	
				texture.setWrap(WrapAxis.R, value);
			}
		});*/
		
		EnumPropertyObject<WrapMode> wrapS = (EnumPropertyObject<WrapMode>)model.getPropertySection("Texture").getProperty("Wrap Mode S").getPropertyObject();
		
		wrapS.SetListener(new I_EnumPropertyObjectListener<WrapMode>() {

			public WrapMode readValue() {
				
				return texture.getWrap(WrapAxis.S);
			}

			public void saveValue(WrapMode value) {
	
				texture.setWrap(WrapAxis.S, value);
			}
		});
		
		EnumPropertyObject<WrapMode> wrapT = (EnumPropertyObject<WrapMode>)model.getPropertySection("Texture").getProperty("Wrap Mode T").getPropertyObject();
		
		wrapT.SetListener(new I_EnumPropertyObjectListener<WrapMode>() {

			public WrapMode readValue() {
				
				return texture.getWrap(WrapAxis.T);
			}

			public void saveValue(WrapMode value) {
	
				texture.setWrap(WrapAxis.T, value);
			}
		});
		
		Vector3fPropertyObject translation = (Vector3fPropertyObject)model.getPropertySection("Texture").getProperty("Translation").getPropertyObject();
		
		translation.SetListener(new I_Vector3fPropertyObjectListener() {

			public Vector3f readValue() {
				
				Vector3f v = texture.getTranslation();
				
				if(v == null)
					texture.setTranslation(new Vector3f());
				
				return texture.getTranslation();
			}

			public void saveValue(Vector3f value) {
	
				texture.setTranslation(value);
			}
		});
		
		QuaternionPropertyObject rotation = (QuaternionPropertyObject)model.getPropertySection("Texture").getProperty("Rotation").getPropertyObject();
		
		rotation.SetListener(new I_QuaternionPropertyObjectListener() {

			public Quaternion readValue() {
				
				Quaternion q = texture.getRotation();
				
				if(q == null)
					texture.setRotation(new Quaternion());
				
				return texture.getRotation();
			}

			public void saveValue(Quaternion value) {
	
				texture.setRotation(value);
			}
		});
		
		Vector3fPropertyObject scale = (Vector3fPropertyObject)model.getPropertySection("Texture").getProperty("Scale").getPropertyObject();
		
		scale.SetListener(new I_Vector3fPropertyObjectListener() {

			public Vector3f readValue() {
				
				Vector3f v = texture.getScale();
				
				if(v == null)
					texture.setScale(new Vector3f(1, 1, 1));
				
				return texture.getScale();
			}

			public void saveValue(Vector3f value) {
	
				texture.setScale(value);
			}
		});

		EnumPropertyObject<CombinerFunctionAlpha> alphaFunction = (EnumPropertyObject<CombinerFunctionAlpha>)model.getPropertySection("Alpha Combine").getProperty("Combiner Funtion").getPropertyObject();
		
		alphaFunction.SetListener(new I_EnumPropertyObjectListener<CombinerFunctionAlpha>() {

			public CombinerFunctionAlpha readValue() {
				
				return texture.getCombineFuncAlpha();
			}

			public void saveValue(CombinerFunctionAlpha value) {
	
				texture.setCombineFuncAlpha(value);
			}
		});

		EnumPropertyObject<CombinerSource> alphaSrc0 = (EnumPropertyObject<CombinerSource>)model.getPropertySection("Alpha Combine").getProperty("Combiner Source 0").getPropertyObject();
		
		alphaSrc0.SetListener(new I_EnumPropertyObjectListener<CombinerSource>() {

			public CombinerSource readValue() {
				
				return texture.getCombineSrc0Alpha();
			}

			public void saveValue(CombinerSource value) {
	
				texture.setCombineSrc0Alpha(value);
			}
		});

		EnumPropertyObject<CombinerOperandAlpha> alphaOp0 = (EnumPropertyObject<CombinerOperandAlpha>)model.getPropertySection("Alpha Combine").getProperty("Combiner Operand 0").getPropertyObject();
		
		alphaOp0.SetListener(new I_EnumPropertyObjectListener<CombinerOperandAlpha>() {

			public CombinerOperandAlpha readValue() {
				
				return texture.getCombineOp0Alpha();
			}

			public void saveValue(CombinerOperandAlpha value) {
	
				texture.setCombineOp0Alpha(value);
			}
		});

		EnumPropertyObject<CombinerSource> alphaSrc1 = (EnumPropertyObject<CombinerSource>)model.getPropertySection("Alpha Combine").getProperty("Combiner Source 1").getPropertyObject();
		
		alphaSrc1.SetListener(new I_EnumPropertyObjectListener<CombinerSource>() {

			public CombinerSource readValue() {
				
				return texture.getCombineSrc1Alpha();
			}

			public void saveValue(CombinerSource value) {
	
				texture.setCombineSrc1Alpha(value);
			}
		});

		EnumPropertyObject<CombinerOperandAlpha> alphaOp1 = (EnumPropertyObject<CombinerOperandAlpha>)model.getPropertySection("Alpha Combine").getProperty("Combiner Operand 1").getPropertyObject();
		
		alphaOp1.SetListener(new I_EnumPropertyObjectListener<CombinerOperandAlpha>() {

			public CombinerOperandAlpha readValue() {
				
				return texture.getCombineOp1Alpha();
			}

			public void saveValue(CombinerOperandAlpha value) {
	
				texture.setCombineOp1Alpha(value);
			}
		});

		EnumPropertyObject<CombinerSource> alphaSrc2 = (EnumPropertyObject<CombinerSource>)model.getPropertySection("Alpha Combine").getProperty("Combiner Source 2").getPropertyObject();
		
		alphaSrc2.SetListener(new I_EnumPropertyObjectListener<CombinerSource>() {

			public CombinerSource readValue() {
				
				return texture.getCombineSrc2Alpha();
			}

			public void saveValue(CombinerSource value) {
	
				texture.setCombineSrc2Alpha(value);
			}
		});

		EnumPropertyObject<CombinerOperandAlpha> alphaOp2 = (EnumPropertyObject<CombinerOperandAlpha>)model.getPropertySection("Alpha Combine").getProperty("Combiner Operand 2").getPropertyObject();
		
		alphaOp2.SetListener(new I_EnumPropertyObjectListener<CombinerOperandAlpha>() {

			public CombinerOperandAlpha readValue() {
				
				return texture.getCombineOp2Alpha();
			}

			public void saveValue(CombinerOperandAlpha value) {
	
				texture.setCombineOp2Alpha(value);
			}
		});
		
		EnumPropertyObject<CombinerScale> alphaScale = (EnumPropertyObject<CombinerScale>)model.getPropertySection("Alpha Combine").getProperty("Combiner Scale").getPropertyObject();
		
		alphaScale.SetListener(new I_EnumPropertyObjectListener<CombinerScale>() {

			public CombinerScale readValue() {
				
				return texture.getCombineScaleAlpha();
			}

			public void saveValue(CombinerScale value) {
				
				texture.setCombineScaleAlpha(value);
			}
		});

		EnumPropertyObject<CombinerFunctionRGB> rgbFunction = (EnumPropertyObject<CombinerFunctionRGB>)model.getPropertySection("RGB Combine").getProperty("Combiner Funtion").getPropertyObject();
		
		rgbFunction.SetListener(new I_EnumPropertyObjectListener<CombinerFunctionRGB>() {

			public CombinerFunctionRGB readValue() {
				
				return texture.getCombineFuncRGB();
			}

			public void saveValue(CombinerFunctionRGB value) {
	
				texture.setCombineFuncRGB(value);
			}
		});

		EnumPropertyObject<CombinerSource> rgbSrc0 = (EnumPropertyObject<CombinerSource>)model.getPropertySection("RGB Combine").getProperty("Combiner Source 0").getPropertyObject();
		
		rgbSrc0.SetListener(new I_EnumPropertyObjectListener<CombinerSource>() {

			public CombinerSource readValue() {
				
				return texture.getCombineSrc0RGB();
			}

			public void saveValue(CombinerSource value) {
	
				texture.setCombineSrc0RGB(value);
			}
		});

		EnumPropertyObject<CombinerOperandRGB> rgbOp0 = (EnumPropertyObject<CombinerOperandRGB>)model.getPropertySection("RGB Combine").getProperty("Combiner Operand 0").getPropertyObject();
		
		rgbOp0.SetListener(new I_EnumPropertyObjectListener<CombinerOperandRGB>() {

			public CombinerOperandRGB readValue() {
				
				return texture.getCombineOp0RGB();
			}

			public void saveValue(CombinerOperandRGB value) {
	
				texture.setCombineOp0RGB(value);
			}
		});

		EnumPropertyObject<CombinerSource> rgbSrc1 = (EnumPropertyObject<CombinerSource>)model.getPropertySection("RGB Combine").getProperty("Combiner Source 1").getPropertyObject();
		
		rgbSrc1.SetListener(new I_EnumPropertyObjectListener<CombinerSource>() {

			public CombinerSource readValue() {
				
				return texture.getCombineSrc1RGB();
			}

			public void saveValue(CombinerSource value) {
	
				texture.setCombineSrc1RGB(value);
			}
		});

		EnumPropertyObject<CombinerOperandRGB> rgbOp1 = (EnumPropertyObject<CombinerOperandRGB>)model.getPropertySection("RGB Combine").getProperty("Combiner Operand 1").getPropertyObject();
		
		rgbOp1.SetListener(new I_EnumPropertyObjectListener<CombinerOperandRGB>() {

			public CombinerOperandRGB readValue() {
				
				return texture.getCombineOp1RGB();
			}

			public void saveValue(CombinerOperandRGB value) {
	
				texture.setCombineOp1RGB(value);
			}
		});

		EnumPropertyObject<CombinerSource> rgbSrc2 = (EnumPropertyObject<CombinerSource>)model.getPropertySection("RGB Combine").getProperty("Combiner Source 2").getPropertyObject();
		
		rgbSrc2.SetListener(new I_EnumPropertyObjectListener<CombinerSource>() {

			public CombinerSource readValue() {
				
				return texture.getCombineSrc2RGB();
			}

			public void saveValue(CombinerSource value) {
	
				texture.setCombineSrc2RGB(value);
			}
		});

		EnumPropertyObject<CombinerOperandRGB> rgbOp2 = (EnumPropertyObject<CombinerOperandRGB>)model.getPropertySection("RGB Combine").getProperty("Combiner Operand 2").getPropertyObject();
		
		rgbOp2.SetListener(new I_EnumPropertyObjectListener<CombinerOperandRGB>() {

			public CombinerOperandRGB readValue() {
				
				return texture.getCombineOp2RGB();
			}

			public void saveValue(CombinerOperandRGB value) {
	
				texture.setCombineOp2RGB(value);
			}
		});
		
		EnumPropertyObject<CombinerScale> rgbScale = (EnumPropertyObject<CombinerScale>)model.getPropertySection("RGB Combine").getProperty("Combiner Scale").getPropertyObject();
		
		rgbScale.SetListener(new I_EnumPropertyObjectListener<CombinerScale>() {

			public CombinerScale readValue() {
				
				return texture.getCombineScaleRGB();
			}

			public void saveValue(CombinerScale value) {
				
				texture.setCombineScaleRGB(value);
			}
		});
	}
}
